const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const secret = require('./secret');
const compression = require('compression');
const history = require('connect-history-api-fallback');


app.use(bodyParser.json({ limit: '20mb' }));
app.use(bodyParser.urlencoded({ limit: '20mb', extended: true }));

app.set('secret', secret)
app.use(require('cors')())
app.use(express.json())

app.use(history({
    htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
    rewrites: [
        { from: /\/admin/, to: '/admin/index.html' },
        { from: /\/download/, to: '/web/api/download' }
    ]
}));
app.use(compression());
app.use('/uploads', express.static(__dirname + '/uploads'));
app.use('/admin', express.static(__dirname + '/admin'));
app.use('/', express.static(__dirname + '/web')) //这样客户端直接访问域名根路径时实际访问的是被托管的web静态文件夹

require('./plugins/db')(app)
require('./routes/admin')(app)
require('./routes/web')(app)


app.listen(3001, () => {
    console.log('listening http://localhost:3001')
})